//
//  JTStartViewController+LiveCheck.m
//  JingRong_Safe_IDLFaceSDKDemoOC
//
//  Created by 万事如意 on 2025/9/1.
//  Copyright © 2025 Baidu. All rights reserved.
//

#import "JTStartViewController+LiveCheck.h"
#import "ViewController+MainSteps.h"
#import "BDIDInfoCollectController.h"
#import "BDIDInfoAutoCollectController.h"
#import <BDFaceBaseKit/BDFaceBaseKit.h>
#import "BDFaceAdjustParamsTool.h"
#import <BDFaceLogicLayer/BDFaceLogicLayer.h>
#import "BDFaceVerificationController.h"

@implementation JTStartViewController (LiveCheck)

- (void)startCheck {
    NSDictionary *tokenDic = @{BDFaceLogicServiceTokenKey : self.accessToken ?: @""};
    __weak typeof(self) this = self;
    self.logicService.showLoadingAction = ^(void) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [this showCheckLiveLoadingAction];
        });
    };
    //设置默认人脸参数
    [self.logicService startRecognizeToCheckLive:tokenDic callBack:^(int resultCode, NSDictionary * _Nonnull resultDic) {
        dispatch_async(dispatch_get_main_queue(), ^{
            // 0 表示成功
            if (resultCode == 0) {
                NSDictionary *responseDic = resultDic[@"data"];
                if (responseDic[@"dec_image"]) {
                    NSString *imgStr = responseDic[@"dec_image"];
                    [self.viewModel requestAttendence:imgStr];
                    [self.navigationController popViewControllerAnimated:YES];
                } else {
                    [self showCheckLiveFailResult:[NSString stringWithFormat:@"%@",responseDic]];
                }
//                if (responseDic[@"log_id"]) {
//                    NSString *log_id = responseDic[@"log_id"];
//                    [self.viewModel requestLivenessAttendenceLogId:log_id];
//                    [self.navigationController popViewControllerAnimated:YES];
//                } else {
//                    [self showCheckLiveFailResult:[NSString stringWithFormat:@"%@",responseDic]];
//                }
            } else {
                NSString *errorMessage = resultDic[BDFaceLogicServiceReturnKeyForResultMsg];
                [self showCheckLiveFailResult:errorMessage];
            }
            
        });
    }];
}

- (void)showCheckLiveLoadingAction {
    BDFaceVerificationController *avc = [[BDFaceVerificationController alloc] init];
    [self.navigationController pushViewController:avc animated:YES];
}

/**
 * 下面的decode_image是最终解密的照片
 */
- (void)showCheckLiveRequestSuccessResult:(NSDictionary *)responseDic {
        BDFaceVerificationController *verificationController = (BDFaceVerificationController *)self.navigationController.topViewController;
    int code = [responseDic[@"error_code"] intValue];
        if (code == 0) {
//            [verificationController showBDFaceSuccess];
//        }else {
            int code = [responseDic[@"error_code"] intValue];
            BDFaceVerificationController *vc = (BDFaceVerificationController *)self.navigationController.topViewController;
            [vc showServerError:code];
        }
}


- (void)showCheckLiveFailResult:(NSString *)errorMessage {
    UIViewController *baseController = self.navigationController.viewControllers[0];
    [self.navigationController popViewControllerAnimated:YES];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [BDFaceToastView showToast:baseController.view text:errorMessage];
    });
}

@end
